Tutorial Brief

This cover the basics of plotting charts and controlling the style and content. We will be using matplotlib library.

Finding Help:

NumPy

Base N-dimensional array package

SciPy

Fundamental library for scientific computing

Matplotlib

Comprehensive 2D Plotting

IPython

Enhanced Interactive Console

SymPy

Symbolic mathematics

Pandas

Data structures & analysis

What can we do with Matplotlib?

Plotting

Scattering

Bars / Histogram

Pie

3D

Images / Contour

Importing the library

Most of the functionality we need to plot charts is in matlotlib.pyplot. Always import it as plt


In [1]:
import matplotlib.pyplot as plt
import numpy as np

Plotting


In [2]:
x = [0,1,2]
y = [0,1,4]

In [3]:
fig = plt.figure()
axes = fig.add_subplot(111)

axes.plot(x, y)

plt.show()


Using NumPy to increase the the number of data points


In [4]:
x_highres = np.linspace(0, 2, 20)
y_highres = x_highres ** 2

In [5]:
fig = plt.figure()
axes = fig.add_subplot(111)

axes.plot(x, y)
axes.plot(x_highres, y_highres)

plt.show()


Controlling Line Style


In [6]:
fig = plt.figure()
axes = fig.add_subplot(111)

axes.plot(x_highres, y_highres, "r--")
#axes.plot(x_highres, y_highres, color="red", linestyle='dashed')

plt.show()



In [7]:
fig = plt.figure()
axes = fig.add_subplot(111)

axes.plot(x_highres, y_highres, color="red", linestyle='dashed',  linewidth=3)

plt.show()



In [8]:
fig = plt.figure()
axes = fig.add_subplot(111)

axes.plot(x_highres, y_highres,color="red", linestyle='dashed',  linewidth=3, marker='o',
         markerfacecolor='blue', markersize=5)

plt.show()


Line Styles & Markers

================    ===============================
character           description
================    ===============================
``'-'``             solid line style
``'--'``            dashed line style
``'-.'``            dash-dot line style
``':'``             dotted line style
``'.'``             point marker
``','``             pixel marker
``'o'``             circle marker
``'v'``             triangle_down marker
``'^'``             triangle_up marker
``'<'``             triangle_left marker
``'>'``             triangle_right marker
``'1'``             tri_down marker
``'2'``             tri_up marker
``'3'``             tri_left marker
``'4'``             tri_right marker
``'s'``             square marker
``'p'``             pentagon marker
``'*'``             star marker
``'h'``             hexagon1 marker
``'H'``             hexagon2 marker
``'+'``             plus marker
``'x'``             x marker
``'D'``             diamond marker
``'d'``             thin_diamond marker
``'|'``             vline marker
``'_'``             hline marker
================    ===============================

Colors

==========  ========
character   color
==========  ========
'b'         blue
'g'         green
'r'         red
'c'         cyan
'm'         magenta
'y'         yellow
'k'         black
'w'         white
==========  ========

In addition, you can specify colors in many weird and
wonderful ways, including full names (``'green'``), hex
strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or
grayscale intensities as a string (``'0.8'``).  Of these, the
string specifications can be used in place of a ``fmt`` group,
but the tuple forms can be used only as ``kwargs``.

Title and Grid


In [9]:
fig = plt.figure()
axes = fig.add_subplot(111)

axes.plot(x_highres, y_highres,color="red", linestyle='dashed',  linewidth=3, marker='o',
         markerfacecolor='blue', markersize=5)

axes.set_title('$y=x^2$') ## Notice you can you LaTeX Code
                          ## for more about LaTeX check the Tutorial about Markdown and LaTeX
axes.grid()

plt.show()


Axis Labels


In [10]:
fig = plt.figure()
axes = fig.add_subplot(111)

axes.plot(x_highres, y_highres,color="red", linestyle='dashed',  linewidth=3, marker='o',
         markerfacecolor='blue', markersize=5)

axes.set_title('$y=x^2$')
axes.grid()
axes.set_xlabel('x')
axes.set_ylabel('y')

plt.show()


Controlling the Figure Size


In [11]:
fig = plt.figure(figsize=(12,8))
axes = fig.add_subplot(111)

axes.plot(x_highres, y_highres,color="red", linestyle='dashed',  linewidth=3, marker='o',
         markerfacecolor='blue', markersize=5)

axes.set_title('$y=x^2$')
axes.grid()
axes.set_xlabel('x')
axes.set_ylabel('y')

plt.show()


Visualizing Numbers as images


In [12]:
noise = np.random.random((128,128))
noise


Out[12]:
array([[ 0.31743783,  0.89968839,  0.64593546, ...,  0.92315307,
         0.40529038,  0.57868866],
       [ 0.03509869,  0.41812159,  0.09614452, ...,  0.35230104,
         0.4358769 ,  0.42696774],
       [ 0.87243532,  0.16665336,  0.30484216, ...,  0.57570625,
         0.41494208,  0.60731167],
       ..., 
       [ 0.8604435 ,  0.91615867,  0.44869023, ...,  0.15850609,
         0.85534071,  0.87021376],
       [ 0.01936096,  0.56630654,  0.49670692, ...,  0.63496359,
         0.15847743,  0.53732171],
       [ 0.67941664,  0.33243944,  0.41832329, ...,  0.7415799 ,
         0.56672038,  0.93639001]])

In [13]:
plt.imshow(noise)
plt.show()


Color Bar


In [14]:
plt.imshow(noise)
plt.colorbar()
plt.show()


Color Maps


In [15]:
plt.imshow(noise, cmap=plt.cm.gray)
plt.colorbar()
plt.show()



In [16]:
plt.imshow(noise, cmap=plt.cm.Paired)
plt.colorbar()
plt.show()